package com.cos.shop.dao.impl;

import com.cos.shop.dao.ItemDao;
import com.cos.shop.entity.Item;
import com.cos.shop.entity.vo.ItemVo;
import com.cos.shop.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: ItemDaoImpl
 * Author: saber
 * Date:2022/9/20 18:40
 */
public class ItemDaoImpl implements ItemDao {
    private QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

    @Override
    public int[] addItemlist(List<Item> items) throws SQLException {
        Object[][] itemArr = new Object[items.size()][];

        for (int i = 0; i <items.size() ; i++) {
            itemArr[i]=new Object[]{
                    items.get(i).getOid(),
                    items.get(i).getPid(),
                    items.get(i).getIcount(),
                    items.get(i).getInum(),
            };
        }
        int[] batch =qr.batch("insert into item values(default,?,?,?,?)",itemArr);
        return batch;
    }

    @Override
    public List<ItemVo> getItemByOid(String oid) throws SQLException {
        return qr.query("select * from item i inner join product p on i.pid=p.pid where oid= ?",
                new BeanListHandler<>(ItemVo.class),oid);
    }
}
